package com.ty.controller.common;

import java.util.HashMap;
import java.util.Map;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/common/public")
public class PublicController {

	//登录的方法
	@RequestMapping("/login")
	@ResponseBody
	public Map<String, Object> login(String username,String password) {
		 Map<String, Object> resultJson=new HashMap<>();
		 //当前用户
		 Subject currentUser= SecurityUtils.getSubject();
		 //验证信息
		 UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(username,password);
		 //登录
		 try {
			 currentUser.login(usernamePasswordToken);
			 if(currentUser.isAuthenticated()) { //表示是否认证成功！
				 resultJson.put("status",1);
				 resultJson.put("msg","登录成功！");
			 }else {
				 resultJson.put("status", 0);
				 resultJson.put("msg","用户名或密码不正确！");
			 }
		 }catch (AuthenticationException e) {
			 resultJson.put("status", 0);
			 resultJson.put("msg","用户名或密码不正确！");
		  }
		
		 return resultJson;
	}
}
